package com.turing.auth_server.controller;

import com.alibaba.fastjson.JSONObject;
import com.nimbusds.jose.JOSEException;
import com.turing.auth_server.utils.JWTUtiles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import result.Result;

import java.text.ParseException;

@RestController
public class AuthController {

    //根据token获取用户信息
    @GetMapping("getUserInfoByToken")
    public Result getUserInfoByToken(@RequestParam("token") String token) throws ParseException {
        return Result.success(JWTUtiles.getUserInfo(token));
    }

    //根据openid创建token
    @GetMapping("createTokenByOpenId")
    public Result createTokenByOpenId(@RequestParam("openId")String openId) throws JOSEException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", openId);
        String payload = jsonObject.toJSONString();
        String token = JWTUtiles.createToken(payload);
        return Result.success(token);
    }
}
